#ifndef __MaestroNano_h
#define __MaestroNano_h

#include <stdio.h>
#include "Driver\DrvGPIO.h"
#include "Driver\DrvPWM.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvADC.h"
#include "Driver\DrvSPI.h"
#include <math.h>

/* System Clock source, Ref. man pg.155-156 */
#define CONFIG_SYS_Clk_XTL12M 0
#define CONFIG_SYS_Clk_OSC22M 4
#define CONFIG_SYS_Clk_PLLFOUT 2

//#define CONFIG_USING_SYS_Clk CONFIG_SYS_Clk_XTL12M	/* user def. */
#define CONFIG_USING_SYS_Clk CONFIG_SYS_Clk_PLLFOUT
//#define CONFIG_USING_SYS_Clk CONFIG_SYS_Clk_OSC22M

#if CONFIG_USING_SYS_Clk==CONFIG_SYS_Clk_PLLFOUT
#define CONFIG_PLLCON_SRC_XTL12M 0
#define CONFIG_PLLCON_SRC_OSC22M 1
#endif
#define DDC114_Start_Conversion() DrvPWM_Enable(DRVPWM_TIMER0, 1)
#define DDC114_Stop_Conversion() DrvPWM_Enable(DRVPWM_TIMER0, 0)
#define Xenon_Flash_On() DrvPWM_Enable(DRVPWM_TIMER3, 1)
#define Xenon_Flash_Off() DrvPWM_Enable(DRVPWM_TIMER3, 0)
//#define CONFIG_DIV_Clkout  /* user def.: output clock output on PB.12 */
#define Measure_phases 2
#define Measure_times 102
//#define DDC114_DEBUG
#define MaestroNano_DEBUG 1
//#define SYS_DEBUG 1
//#define Adjust_Intensity 1
#define MaestroNano_Fit_Mode 1
#define MaestroNano_OD_Allow_Negative 1
#define MaestroNano_Algo 1
extern void MaestroNano_Init(void);
extern double MaestroNano_Fit_Value(double Measure_OD, int type);
extern volatile uint32_t adc_data_ready;
extern int adc_data[Measure_phases][Measure_times], adc_data1[Measure_phases][Measure_times], adc_data2[Measure_phases][Measure_times];
extern int most_count, range_start_index, range_end_index; 

//polynomial curve fitting, support max 5 curves
//#define USE_FITTING_CURVE 1
struct Curve_Param_t {
  double Threhold[5];
  double Polynomial_Coeff[5][4];
};
struct MaestroNano_Param_t {
#ifdef USE_FITTING_CURVE
  struct Curve_Param_t A260_curve, A280_curve;
  double factor_230, factor_280, factor_260;
#else  //piecewise-linear model
    double OD260[15], A260[15];
	float  OD280[15], A280[15], A260_A280[15], A260_A230[15];
#endif
  unsigned char check_digits[16];
};

#ifdef USE_FITTING_CURVE
  extern struct Curve_Param_t A260_curve, A280_curve;
#else
  extern double OD260[15], A260[15];
  extern float OD280[15], A280[15], A260_A280[15], A260_A230[15];
  extern double factor_230, factor_280, factor_260;
#endif
#endif